<?php 
/**
 * 功能函数
 */
if(!defined('EMLOG_ROOT')) {exit('error!');} 
?>
<?php 
//虾米优酷数据缓存函数(包含各种信息)
function Ting_d_cache($log_content, $logid, $type){
	preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
    $posturl=htmlspecialchars($embedsrc);
	if($posturl){
		if(strstr($posturl,"youku.com/")){
			$youkuData = YoukuData($log_content);
			$Data = $youkuData['high']['0'];
				Cache_w($Data, $logid);
		}if(strstr($posturl,"xiami.com/")){
			$url = 'http://www.xiami.com/widget/xml-single/sid/'.XiamiId($log_content).'';
			$Date = file_get_contents($url);
			$xmtitle = str_substr('<song_name><![CDATA[',']]></song_name>', $Date);
			$xmname = str_substr('<artist_name><![CDATA[',']]></artist_name>', $Date);
			$xmalbum_name = str_substr('<album_name><![CDATA[',']]></album_name>', $Date);
			$xmalbum_id = str_substr('<album_id>','</album_id>', $Date);
			$xmurl = ipcxiami(str_substr('<location><![CDATA[',']]></location>', $Date));
			$ting_cache[$logid] = array(
				'xmtitle' => $xmtitle,
				'xmname' => $xmname,
				'xmalbum_name' => $xmalbum_name,
				'xmalbum_id' => $xmalbum_id,
				'xmlogid' => $logid,
				);
				Cache_w($ting_cache[$logid], $logid);
				if($type == 'xmurl'){
					Cache_w($xmurl, ''.$logid.'_url');
				}
		}
	}
}
//缓存写入
function Cache_w($Data, $logid){
	$file_path=EMLOG_ROOT.'/content/cache/ting'; //缓存文件夹路径
	if(!file_exists($file_path)){
		mkdir($file_path,0755);
		}
		$file=$file_path.'/'.$logid.'.php'; //绝对路径
		$file_url=BLOG_URL.'/content/cache/ting/'.$logid.'.php'; //相对路径
		     $Data = "<?php exit;//" . serialize($Data);
		     @ $fp = fopen($file, 'wb') OR emMsg('读取缓存失败。如果您使用的是Unix/Linux主机，请修改缓存目录 (content/cache/ting) 下所有文件的权限为777。如果您使用的是Windows主机，请联系管理员，将该目录下所有文件设为可写');
		     @ $fw = fwrite($fp, $Data) OR emMsg('写入缓存失败，缓存目录 (content/cache/ting) 不可写');
		     fclose($fp);
}
//缓存读取
function Cache_r($log_content, $logid, $type){
	preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
    $posturl=htmlspecialchars($embedsrc);
	$file_path=EMLOG_ROOT.'/content/cache/ting'; //缓存文件夹路径
    $file=$file_path.'/'.$logid.'.php'; //绝对路径
	$url_file=$file_path.'/'.$logid.'_url.php'; //绝对路径
	if($type == 'xmurl'){
		$time_xiami = 60*60*8; //缓存时间为8小时
		if(!file_exists($url_file) || filesize($url_file) <= 0 || (time()-filemtime($url_file)) > $time_xiami){
			Ting_d_cache($log_content, $logid, 'xmurl');
		}
		if ($fp = fopen($url_file, 'r')) {
			$data = fread($fp, filesize($url_file));
			fclose($fp);
			clearstatcache();
			$logid_cache = unserialize(str_replace("<?php exit;//", '', $data)); 
			if($logid_cache == null){
				Ting_d_cache($log_content, $logid, 'xmurl');
			}
			echo $logid_cache;
		}
	}
	if($posturl){
		if(strstr($posturl,"xiami.com/")){
				if(!file_exists($file) || filesize($file) <= 0){
					Ting_d_cache($log_content, $logid, 'other');
				}
				if ($fp = fopen($file, 'r')) { 
						$data = fread($fp, filesize($file)); 
						fclose($fp); 
						clearstatcache(); 
						$logid_cache = unserialize(str_replace("<?php exit;//", '', $data));
						if($logid_cache == null){
							echo Ting_d_cache($log_content, $logid, 'other'); 
						}
						if($type == 'xmname'){
						echo $logid_cache['xmname'];
						}if($type == 'xmtitle'){
							echo $logid_cache['xmtitle'];
						}if($type == 'xmalbum_name'){
							echo $logid_cache['xmalbum_name'];
						}if($type == 'xmalbum_id'){
							echo $logid_cache['xmalbum_id'];
						}if($type == 'xmlogid'){
							echo $logid_cache['xmlogid'];
						}
				}
			}
		if(strstr($posturl,"youku.com/")){
			$time_youku = 60*60*5; //缓存时间为5小时
			if(!file_exists($file) || filesize($file) <= 0 || (time()-filemtime($file)) > $time_youku){
				Ting_d_cache($log_content, $logid, 'other'); 
				}
				if ($fp = fopen($file, 'r')) { 
					$data = fread($fp, filesize($file)); 
					fclose($fp); 
					clearstatcache(); 
					$logid_cache = unserialize(str_replace("<?php exit;//", '', $data)); 
					if($logid_cache == null){
						Ting_d_cache($log_content, $logid, 'other'); 
					}
					echo $logid_cache; 
				}
			}
	}
}
//缩略图函数
function TingImage($log_content, $logid){
	preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
    $posturl=htmlspecialchars($embedsrc);
	if($posturl){
	if(strstr($posturl,"youku.com/")){
		echo YoukuImg($log_content, $logid);
	}if(strstr($posturl,"xiami.com/")){
	    echo XiamiImg($log_content, $logid);
	}if(strstr($posturl,"yinyuetai.com/")){
	    echo yinyuetaiImg($log_content, $logid);
	}
	}else{
		echo XiamiArtistImg($log_content, $logid);
	}
}
//虾米id
function XiamiId($log_content){ 
	preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
	$id = str_substr('t/0_', '/sing', $embedsrc);
	return $id;
}
//虾米缩略图
function XiamiImg($log_content, $logid){
	$file_path=EMLOG_ROOT."/content/uploadfile/ting/xiami/"; //缓存文件夹路径
	if(!file_exists($file_path)){
			mkdir($file_path,0755);
			}
	$image_path=$file_path."/tn_xiami_".$logid.'.jpg'; //绝对路径 
	$image_url=BLOG_URL."content/uploadfile/ting/xiami/tn_xiami_".$logid.'.jpg'; //相对路径
	if(file_exists($image_path)){//文件存在
		echo $image_url;
		}else{
			$url = 'http://www.xiami.com/widget/xml-single/sid/'.XiamiId($log_content).'';
			$Date = file_get_contents($url);
			$img = str_substr('<album_cover><![CDATA[','_3.jpg', $Date);
			$src = ''.$img.'.jpg';
			copy($src,$image_path);
			resizeImage($image_url, $image_path, '280', '280');
			}
}
//虾米音乐相关数据
function XiamiInfo($log_content, $file_type, $logid){
	$url = 'http://www.xiami.com/widget/xml-single/sid/'.XiamiId($log_content).'';
	$Date = file_get_contents($url);
	if($file_type == 'xmname'){
		$xmname = str_substr('<artist_name><![CDATA[',']]></artist_name>', $Date);
		echo Cache_w($xmname, 'xmname', $logid);
	}if($file_type == 'xmalbum'){
		$xmalbum = str_substr('<album_name><![CDATA[',']]></album_name>', $Date);
		echo Cache_w($xmalbum, 'xmalbum', $logid);
	}if($file_type == 'xmurl'){
		$xmurl = str_substr('<location><![CDATA[',']]></location>', $Date);
		$xmsrc = ipcxiami($xmurl);
		echo Cache_w($xmsrc, 'xmurl', $logid);
	}
}
//虾米地址解析函数
function ipcxiami($location){
$count = (int)substr($location, 0, 1);
$url = substr($location, 1);
$line = floor(strlen($url) / $count);
$loc_5 = strlen($url) % $count;
$loc_6 = array();
$loc_7 = 0;
$loc_8 = '';
$loc_9 = '';
$loc_10 = '';
while ($loc_7 < $loc_5){
$loc_6[$loc_7] = substr($url, ($line+1)*$loc_7, $line+1);
$loc_7++;
}
$loc_7 = $loc_5;
while($loc_7 < $count){
$loc_6[$loc_7] = substr($url, $line * ($loc_7 - $loc_5) + ($line + 1) * $loc_5, $line);
$loc_7++;
}
$loc_7 = 0;
while ($loc_7 < strlen($loc_6[0])){
$loc_10 = 0;
while ($loc_10 < count($loc_6)){
$loc_8 .= @$loc_6[$loc_10][$loc_7];
$loc_10++;
}
$loc_7++;
}
$loc_9 = str_replace('^', 0, urldecode($loc_8));
return $loc_9;
}
//音悦台缩略图
function YinyuetaiImg($log_content, $logid){
	$file_path=EMLOG_ROOT."/content/uploadfile/ting/yinyuetai/"; //缓存文件夹路径
	if(!file_exists($file_path)){
			mkdir($file_path,0755);
			}
	$image_path=$file_path."/tn_yinyuetai_".$logid.'.jpg'; //绝对路径 
	$image_url=BLOG_URL."content/uploadfile/ting/yinyuetai/tn_yinyuetai_".$logid.'.jpg'; //相对路径
	if(file_exists($image_path)){//文件存在
		echo $image_url;
		}else{
			preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	        $embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
			$id = str_substr('/player/', '/v_0.swf', $embedsrc);
			$url = 'http://v.yinyuetai.com/video/'.$id.'';
			$Date = file_get_contents($url);
			//获取标题
            preg_match('/property="og:title"[\s]+content="([^"]*)".*?>/i',$Date,$title);
            //获取封面
            preg_match('/property="og:image" content="([^"]*)".*?>/',$Date,$images);
            //获取MV的ID
            preg_match('/[\d]+/',$url,$song_id);
            $title=$title[1];
            $images=$images[1];
            $song_id=$song_id[0];
			$img = str_substr('<meta property="og:image" content="','"/>', $Date);
			copy($img,$image_path);
			resizeImage($image_url, $image_path, '800', '252');
			}
}
//优酷视频url
function YoukuData($log_content){
    preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
	if (preg_match('{/sid/(\w+)/}', $embedsrc, $matches)) {
		$id = $matches[1];
		}else{
			$id = str_substr('/sid/', '/v.swf', $embedsrc);
			}$url = "http://v.youku.com/v_show/id_$id.html";
			$Data = Youku::parse($url);
			return $Data;
}
//优酷视频截图
function YoukuImg($log_content, $logid){
	$file_path=EMLOG_ROOT."/content/uploadfile/ting/youku/"; //缓存文件夹路径
    if(!file_exists($file_path)){
        mkdir($file_path,0755);
        }
    $image_path=$file_path."/tn_youku_".$logid.'.jpg'; //绝对路径
	$image_url=BLOG_URL."content/uploadfile/ting/youku/tn_youku_".$logid.'.jpg'; //相对路径
	if(file_exists($image_path)){//图片存在
	return $image_url;
	}else{
		$Data = YoukuData($log_content);
		copy($Data['img'],$image_path);
		resizeImage($image_url, $image_path, '800', '252');
		}
}
//优酷视频相关信息
function TingMvInfo($log_content,$file_type,$logid){
	preg_match_all("|<embed[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $swf);
	$embedsrc = !empty($swf[1]) ? $swf[1][0] : '';
    $posturl=htmlspecialchars($embedsrc);
	if(strstr($posturl,"youku.com/")){ 
		$youkuData = YoukuData($log_content); 
		//if($youkuData['super']['0']){ 
			$Data = $youkuData['super']['0']; 
			echo Cache_w($Data, $file_type, $logid); 
			//}if($youkuData['high']['0']){ 
				//$Data = $youkuData['high']['0']; 
				//echo Cache_w($Data, $file_type, $logid); 
				//}else{ 
					//$Data = $youkuData['normal']['0']; 
					//echo Cache_w($Data, $file_type, $logid); 
					//}
	}if(strstr($posturl,"yinyuetai.com/")){
		$time=$_SERVER['REQUEST_TIME'];
		$id = str_substr('/player/', '/v_0.swf', $embedsrc);
        //解析json
         $songurl = "http://www.yinyuetai.com/api/info/get-video-urls?callback=callback&videoId=".$id."&_=".$time;
         $sdata = file_get_contents($songurl);
         if (strpos($sdata, "callback") !== false){
           $lpos = strpos($sdata, "(");
           $rpos = strrpos($sdata, ")");
           $sdata  = substr($sdata, $lpos + 1, $rpos - $lpos -1);
		   }
		   $json= json_decode($sdata,true);
		   //echo "流畅地址：".$json['hcVideoUrl']."<br/>";
           //echo "高清地址：".$json['hdVideoUrl']."<br/>";
           //echo "超清地址：".$json['heVideoUrl']."<br/>";
		   //$Data = 'http://api.520cv.com/yyt.php?id='.$id.'';
		   $Data = $json['heVideoUrl'];
			echo Cache_w($Data, $file_type, $logid);
	}
}
//截断获取
function str_substr($start, $end, $log_content){
    $temp = explode($start, $log_content, 2);
    $content = explode($end, $temp[1], 2);
    return $content[0];
}
//艺人档案图片
function XiamiArtistImg($log_content, $logid){
	$file_path=EMLOG_ROOT."/content/uploadfile/ting/artist/"; //缓存文件夹路径
	if(!file_exists($file_path)){
			mkdir($file_path,0755);
			}
	$image_path=$file_path."/tn_artist_".$logid.'.jpg'; //绝对路径 
	$image_url=BLOG_URL."content/uploadfile/ting/artist/tn_artist_".$logid.'.jpg'; //相对路径
	if(file_exists($image_path)){//文件存在
		echo $image_url;
		}else{
	        preg_match_all("|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $log_content, $img);
	        $imgsrc = !empty($img[1]) ? $img[1][0] : '';
			copy($imgsrc,$image_path);
			resizeImage($image_url, $image_path, '800', '280');
			}
}
//分类
function Sort_name($blogid){
	global $CACHE; 
	$log_cache_sort = $CACHE->readCache('logsort');
	?>
	<?php if(!empty($log_cache_sort[$blogid])): ?>
    <a href="<?php echo Url::sort($log_cache_sort[$blogid]['id']); ?>"><?php echo $log_cache_sort[$blogid]['name']; ?></a>
	<?php endif;
}
//去除img.embed,baidupan,bt
function nolog_style($log_content){
	  $log_content = preg_replace("/\[baidu=(http|https|ftp|file){1}(:\/\/)?([\da-z-\.]+)\.([a-z]{2,6})([\/\w \.-?&%-=]*)*\/?\]/", '', $log_content);
	  $log_content = preg_replace("/\[bt=(http|https|ftp|file){1}(:\/\/)?([\da-z-\.]+)\.([a-z]{2,6})([\/\w \.-?&%-=]*)*\/?\]/", '', $log_content);
	  $log_content = preg_replace("/<img([^>]+?)>/i",'',$log_content);
      $log_content = preg_replace("/<embed([^>]+?)>/i",'',$log_content);
	  return $log_content;
}
//去除html样式
function nohtml($content){
  $content = preg_replace("/<a[^>]*>/i",'', $content);   
  $content = preg_replace("/<\/a>/i", '', $content);    
  $content = preg_replace("/<div[^>]*>/i",'', $content);   
  $content = preg_replace("/<\/div>/i",'', $content);
  $content = preg_replace("/<font[^>]*>/i",'', $content);   
  $content = preg_replace("/<\/font>/i",'', $content);
  //$content = preg_replace("/<p[^>]*>/i",'', $content);   
  //$content = preg_replace("/<\/p>/i",'', $content);
  $content = preg_replace("/<span[^>]*>/i",'', $content);   
  $content = preg_replace("/<\/span>/i",'', $content);
  $content = preg_replace("/<\?xml[^>]*>/i",'', $content);
  $content = preg_replace("/<\/\?xml>/i",'', $content);
  $content = preg_replace("/<o:p[^>]*>/i",'', $content);
  $content = preg_replace("/<\/o:p>/i",'', $content);
  $content = preg_replace("/<u[^>]*>/i",'', $content);
  $content = preg_replace("/<\/u>/i",'', $content);
  $content = preg_replace("/<b[^>]*>/i",'', $content);
  $content = preg_replace("/<\/b>/i",'', $content); 
  $content = preg_replace("/<meta[^>]*>/i",'', $content);
  $content = preg_replace("/<\/meta>/i",'', $content);
  $content = preg_replace("/<!--[^>]*-->/i",'', $content);//注释内容  
  $content = preg_replace("/<p[^>]*-->/i",'', $content);//注释内容       
  //$content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式   
  $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式   
  $content = preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式      
  $content = preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式       
  $content = preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式    
  $content = preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式    
  $content = preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式    
  $content = preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式 
  $content = preg_replace("/<img([^>]+?)>/i",'',$content);
  $content = preg_replace("/<embed([^>]+?)>/i",'',$content);
  $content = preg_replace("/face=.+?['|\"]/",'',$content);
  $content = str_replace('阅读全文&gt;&gt;', '', $content);
  $content = str_replace( "&nbsp;","",$content);
  return $content;
}
//blog-tool:判断是否是首页
function if_is_home(){
    if (BLOG_URL . trim(Dispatcher::setPath(), '/') == BLOG_URL){
        return true;
    } else {
        return FALSE;
    }
}
//带链接关键词
function ting_tags($blogid){
	global $CACHE;
	$log_cache_tags = $CACHE->readCache('logtags');
	if (!empty($log_cache_tags[$blogid])){
		$tag = '&nbsp;&nbsp;';
		foreach ($log_cache_tags[$blogid] as $value){
			$tag .= "	<a href=\"".Url::tag($value['tagurl'])."\">".$value['tagname'].'</a>';
		}
		echo $tag;
	}
}
//列表页自定义分页函数
function my_page($count, $perlogs, $page, $url, $anchor = '') {
 $pnums = @ceil($count / $perlogs);
 $re = '';
 $urlHome = preg_replace("|[\?&/][^\./\?&=]*page[=/\-]|", "", $url);
 if($page > 1) {
  $i = $page - 1;
  $re = " <li><a href=\"".$url.$i."\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\"><i class=\"fa fa-chevron-left\"></i></a></li> " . $re;
 }
 for ($i = $page - 4; $i <= $page + 4 && $i <= $pnums; $i++) {
		if ($i > 0) {
			if ($i == $page) {
				$re .= " <li class=\"active\"><a>$i</a></li> ";
			} elseif ($i == 1) {
				$re .= " <li><a href=\"$urlHome$anchor\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\">$i</a></li> ";
			} else {
				$re .= " <li><a href=\"$url$i$anchor\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\">$i</a></li> ";
			}
		}
	}
	if ($page > 5)
		$re = "<li><a href=\"{$urlHome}$anchor\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\" title=\"home\"></a></li>$re";
	if($page < $pnums) {
  $i = $page + 1;
  $re .= " <li><a href=\"".$url.$i."\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\"><i class=\"fa fa-chevron-right\"></i></a></li> ";
 }
	if ($page + 4 < $pnums)
		$re .= "<li class=\"all\"><a href=\"$url$pnums$anchor\" data-bjax data-target=\"#bjax-target\" data-el=\"#bjax-el\" data-replace=\"true\" title=\"last\"></a></li>";
	if ($pnums <= 1)
		$re = '';
 return $re;
}
//log_xiami_list：导航
function xiami_navi(){
	global $CACHE; 
	$navi_cache = $CACHE->readCache('navi');
	?>
	
	<?php
	foreach($navi_cache as $value):

        if ($value['pid'] != 0) {
            continue;
        }
		$newtab = $value['newtab'] == 'y' ? 'target="_blank"' : '';
        $value['url'] = $value['isdefault'] == 'y' ? BLOG_URL . $value['url'] : trim($value['url'], '/');
        $current_tab = BLOG_URL . trim(Dispatcher::setPath(), '/') == $value['url'] ? 'current' : 'common';
		?>
			<a href="<?php echo $value['url']; ?>" data-bjax data-target="#bjax-target" data-el="#bjax-el" data-replace="true" class="list-group-item" <?php echo $newtab;?>>
						<?php echo $value['naviname']; ?></a>
			<?php if (!empty($value['children'])) :?>
                <?php foreach ($value['children'] as $row){
                        echo '<a href="'.Url::sort($row['sid']).'" data-bjax data-target="#bjax-target" data-el="#bjax-el" data-replace="true" class="list-group-item">'.$row['sortname'].'</a>';
                }?>
            <?php endif;?>

            <?php if (!empty($value['childnavi'])) :?>
                <?php foreach ($value['childnavi'] as $row){
                        $newtab = $row['newtab'] == 'y' ? 'target="_blank" class="list-group-item"' : '';
                        echo '<a href="' . $row['url'] . ' "\" $newtab data-bjax data-target="#bjax-target" data-el="#bjax-el" data-replace="true" >" ' . $row['naviname'].'</a>';
                }?>
            <?php endif;?>
	<?php endforeach; ?>
<?php }
//验证email是否有对应的gravatar头像
function validate_gravatar($Verification_email) {
      $email = $Verification_email; // 要验证的Email地址   
      $hash = md5(strtolower(trim($email)));   
      //$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';  
	  //$uri = 'http://gravatar.duoshuo.com/avatar/' . $hash . '?d=404';
	  $uri = 'https://secure.gravatar.com/avatar/' . $hash . '?d=404';
      $headers = @get_headers($uri);   
      if (!preg_match("|200|", $headers[0])) {   
                return FALSE;
      } else {   
                return true;
      }
}
    function get_template_name(){
        $template_name = str_replace(BLOG_URL,"",TEMPLATE_URL);
        $template_name = str_replace("content/templates/","",$template_name);
        $template_name = str_replace("/","",$template_name);
        return $template_name;
    }
//gravatar头像缓存，无头像随机抽取
function ting_avatar( $email) {
  if(validate_gravatar($email)=='true'){
	  $gfile_path=EMLOG_ROOT."/content/uploadfile/ting/avatar/"; //缓存文件夹路径 
	  if(!file_exists($gfile_path)){
		  mkdir($gfile_path,0755); 
		  } 
		  $gmd5 = md5( strtolower( $email ) );//根据email的值来生成一个md5变量值，作为本地.jpg头像的名字 
		  $gimg = BLOG_URL."content/uploadfile/ting/avatar/g_" . $gmd5. ".jpg";//avatar相对路径 
		  $gfile = $gfile_path.'g_'. $gmd5. '.jpg';//缓存的头像的绝对路径 
		  $gtime = 24*3600*365; //单位s，设置更新时间为15天
		  if (!file_exists($gfile) || (time()-filemtime($gfile)) > $gtime){ //不是第一次留言留言且留言时间超过15天就更新头像 
			  //$gimg_url = 'http://www.gravatar.com/avatar/'. $gmd5; // 旧服务器 (哪个快就开哪个) 
			  //$gimg_url = 'http://gravatar.duoshuo.com/avatar/'. $gmd5;
			  $gimg_url = 'https://secure.gravatar.com/avatar/'. $gmd5;
			  copy($gimg_url, $gfile); //将$gimg_url代表的网络图片复制到$gfile代表的本地服务器中 
			  } 
			  }else{$gimg = TEMPLATE_URL .'images/ting.png';//自定义的头像 
			  } 
			  echo $gimg;
}
?>